import {
    reqSpaceManager,
    reqOrgList,
    reqSmokeAlarmConfigAdd,
    reqTenantList,
    reqTerminalList
} from "@/api/api";
import { mapState } from "vuex";

export default {
    data() {
        return {
            showPopover: false,
            searchForm: {
                targetType: '',
                targetId: ''
            },
            addInfo: {
                tenantId: '',
                orgId: '',
                parentId: '',
                floorId: '',
                spaceId: '',
                deviceId: ''
            },
            targetTypeList: [{
                    id: 1,
                    name: '租户'
                }, {
                    id: 2,
                    name: '组织机构'
                },
                //  {
                //     id: 3,
                //     name: '建筑物'
                // }, {
                //     id: 4,
                //     name: '楼层'
                // }, {
                //     id: 5,
                //     name: '安装点'
                // },
                {
                    id: 6,
                    name: '设备'
                },
            ],

            // 租户
            enterpriseList: [],
            sysInstanceIndex: 1,
            sysInstanceTotalPages: 10,
            loading: false,
            tenantIdflag: true,
            tenantFlag: false,

            // 单位
            orgList: [],
            sysInstanceIndexorg: 1,
            sysInstanceTotalPagesorg: 10,
            loadingorg: false,
            orgFlag: false,

            // 建筑物
            buildings: [],
            sysInstanceIndexParent: 1,
            sysInstanceTotalPagesParent: 10,
            loadingParent: false,
            parentFlag: false,

            // 楼层
            floor: [],
            sysInstanceIndexfloor: 1,
            sysInstanceTotalPagesfloor: 10,
            loadingfloor: false,
            floorFlag: false,

            // 安装点
            spaceList: [],
            sysInstanceIndexspace: 1,
            sysInstanceTotalPagesspace: 10,
            loadingspace: false,
            spaceFlag: false,

            // 设备
            deviceList: [],
            sysInstanceIndexdevice: 1,
            sysInstanceTotalPagesdevice: 10,
            loadingDevice: false,
            deviceFlag: false,
            deviceIdflag: true,

        };
    },
    mounted() {},
    computed: {
        ...mapState(["user"]),
    },
    methods: {
        async remoteMethod(query) {
            if (query != '') {
                var res = await reqTerminalList(this.sysInstanceIndexdevice, 10, {
                    productId: '1612276823406194689',
                    imei: query
                });
                // console.log(res);
                if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
                this.deviceList = res.data.rows || []
            } else {
                this.deviceList = []
            }
        },
        targetTypeChange(e) {
            // console.log(e);
            this.addInfo.tenantId = ''
            this.tenantFlag = false;
            this.orgFlag = false;
            this.parentFlag = false;
            this.floorFlag = false;
            this.spaceFlag = false;
            this.deviceFlag = false;
            if (e == 1) {
                this.tenantFlag = true;
            } else if (e == 2) {
                this.tenantFlag = true;
                this.orgFlag = true;
            } else if (e == 3) {
                this.tenantFlag = true;
                this.orgFlag = true;
                this.parentFlag = true
            } else if (e == 4) {
                this.tenantFlag = true;
                this.orgFlag = true;
                this.parentFlag = true
                this.floorFlag = true;
            } else if (e == 5) {
                this.tenantFlag = true;
                this.orgFlag = true;
                this.parentFlag = true
                this.floorFlag = true;
                this.spaceFlag = true;
            } else if (e == 6) {
                this.deviceFlag = true;
            }
        },
        // 下拉请求列表
        tenantIdChange(val) {
            if (val && this.tenantIdflag) {
                this.getEnterpriseList();
                this.tenantIdflag = false; //只执行一次
            }
        },
        // 获取租户列表
        async getEnterpriseList() {
            var res = await reqTenantList(this.sysInstanceIndex, 10);
            if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
            this.sysInstanceTotalPages = res.data.total
            this.enterpriseList = [...this.enterpriseList, ...res.data.rows]
        },
        async loadMore() {
            if (this.sysInstanceIndex >= Math.ceil(this.sysInstanceTotalPages / 10)) return
            this.sysInstanceIndex++
                this.loading = true;
            // 获取列表数据的方法
            await this.getEnterpriseList()
            this.loading = false;
        },
        // 租户切换
        tenantChange() {
            this.orgList = []
            this.sysInstanceIndexorg = 1
            this.sysInstanceTotalPagesorg = 10
            this.addInfo.orgId = ''

            this.buildings = []
            this.sysInstanceIndexParent = 1
            this.sysInstanceTotalPagesParent = 10
            this.addInfo.parentId = ''

            this.floor = []
            this.sysInstanceIndexfloor = 1
            this.sysInstanceTotalPagesfloor = 10
            this.addInfo.floorId = ''

            this.spaceList = []
            this.sysInstanceIndexspace = 1
            this.sysInstanceTotalPagesspace = 10
            this.addInfo.spaceId = ''

            // this.getOrgList()
            this.$nextTick(() => {
                this.$refs.refTreeSearch.onclose()
                this.$refs.refTreeSearch.getDataList()
            })
        },


        // 请求单位列表
        async getOrgList() {
            const res = await reqOrgList({
                tenantId: this.addInfo.tenantId,
                userId: this.user.userId,
                roleName: 'TENANT_ADMIN',
            });
            if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
            this.orgList = res.data.rows
        },
        async loadMoreorg() {
            if (this.sysInstanceIndexorg >= Math.ceil(this.sysInstanceTotalPagesorg / 10)) return
            this.sysInstanceIndexorg++
                this.loadingorg = true;
            // 获取列表数据的方法
            await this.getOrgList()
            this.loadingorg = false;
        },
        orgIdChange() {
            this.buildings = []
            this.sysInstanceIndexParent = 1
            this.sysInstanceTotalPagesParent = 10
            this.addInfo.parentId = ''

            this.floor = []
            this.sysInstanceIndexfloor = 1
            this.sysInstanceTotalPagesfloor = 10
            this.addInfo.floorId = ''

            this.spaceList = []
            this.sysInstanceIndexspace = 1
            this.sysInstanceTotalPagesspace = 10
            this.addInfo.spaceId = ''
            this.SpaceManager2()
        },

        // 建筑物列表
        async SpaceManager2() {
            let res = await reqSpaceManager(this.sysInstanceIndexParent, 10, {
                userId: this.user.userId,
                roleName: 'TENANT_ADMIN',
                metaCode: 1,
                orgId: this.addInfo.orgId,
                isStatistics: 0
            });
            if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
            this.sysInstanceTotalPagesParent = res.data.total
            this.buildings = [...this.buildings, ...res.data.rows]
        },
        async loadMoreparent() {
            if (this.sysInstanceIndexParent >= Math.ceil(this.sysInstanceTotalPagesParent / 10)) return
            this.sysInstanceIndexParent++
                this.loadingParent = true;
            // 获取列表数据的方法
            await this.SpaceManager2()
            this.loadingParent = false;
        },
        parentIdChange() {
            this.floor = []
            this.sysInstanceIndexfloor = 1
            this.sysInstanceTotalPagesfloor = 10
            this.addInfo.floorId = ''

            this.spaceList = []
            this.sysInstanceIndexspace = 1
            this.sysInstanceTotalPagesspace = 10
            this.addInfo.spaceId = ''
            this.SpaceManager()
        },

        // 楼层列表
        async SpaceManager() {
            let res = await reqSpaceManager(this.sysInstanceIndexfloor, 10, {
                userId: this.user.userId,
                roleName: 'TENANT_ADMIN',
                metaCode: 2,
                orgId: this.addInfo.orgId,
                parentId: this.addInfo.parentId, //建筑物id
                isStatistics: 0
            });
            if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
            this.sysInstanceTotalPagesfloor = res.data.total
            this.floor = [...this.floor, ...res.data.rows]
        },
        async loadMorefloor() {
            if (this.sysInstanceIndexfloor >= Math.ceil(this.sysInstanceTotalPagesfloor / 10)) return
            this.sysInstanceIndexfloor++
                this.loadingfloor = true;
            // 获取列表数据的方法
            await this.SpaceManager()
            this.loadingfloor = false;
        },
        floorIdChange() {
            this.spaceList = []
            this.sysInstanceIndexspace = 1
            this.sysInstanceTotalPagesspace = 10
            this.getSpaceList()
        },

        // 获取安装点列表
        async getSpaceList() {
            let res = await reqSpaceManager(this.sysInstanceIndexspace, 10, {
                userId: this.user.userId,
                roleName: 'TENANT_ADMIN',
                orgId: this.addInfo.orgId,
                parentId: this.addInfo.floorId,
                metaCode: 3,
                isStatistics: 0
            });
            if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
            this.sysInstanceTotalPagesspace = res.data.total
            this.spaceList = [...this.spaceList, ...res.data.rows]
        },
        async loadMorespace() {
            if (this.sysInstanceIndexspace >= Math.ceil(this.sysInstanceTotalPagesspace / 10)) return
            this.sysInstanceIndexspace++
                this.loadingspace = true;
            // 获取列表数据的方法
            await this.getSpaceList()
            this.loadingspace = false;
        },

        // 设备
        deviceChange(val) {
            // if (val && this.deviceIdflag) {
            //     this.getDeviceList();
            //     this.deviceIdflag = false; //只执行一次
            // }
        },
        // 获取设备列表
        async getDeviceList() {
            var res = await reqTerminalList(this.sysInstanceIndexdevice, 10, {
                productId: '1612276823406194689'
            });
            if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
            this.sysInstanceTotalPagesdevice = res.data.total
            this.deviceList = [...this.deviceList, ...res.data.rows]
        },
        async loadMoredevice() {
            if (this.sysInstanceIndexdevice >= Math.ceil(this.sysInstanceTotalPagesdevice / 10)) return
            this.sysInstanceIndexdevice++
                this.loadingDevice = true;
            // 获取列表数据的方法
            await this.getDeviceList()
            this.loadingDevice = false;
        },

        // 搜索
        async searchClick1() {
            this.page = 1;
            this.limit = 10;
            if (this.searchForm.targetType == 1) {
                this.searchForm.targetId = this.addInfo.tenantId
            } else if (this.searchForm.targetType == 2) {
                this.searchForm.targetId = this.addInfo.orgId
            } else if (this.searchForm.targetType == 3) {
                this.searchForm.targetId = this.addInfo.parentId
            } else if (this.searchForm.targetType == 4) {
                this.searchForm.targetId = this.addInfo.floorId
            } else if (this.searchForm.targetType == 5) {
                this.searchForm.targetId = this.addInfo.spaceId
            } else if (this.searchForm.targetType == 6) {
                this.searchForm.targetId = this.addInfo.deviceId
            }
            this.showPopover = false;
            await this.getDataList();
        },
        // 高级搜索重置
        async resetClick() {
            this.page = 1;
            this.limit = 10;
            this.searchForm = {
                targetType: '',
                targetId: ''
            }
            this.addInfo = {
                tenantId: '',
                orgId: '',
                parentId: '',
                floorId: '',
                spaceId: '',
                deviceId: ''
            }
            this.orgList = []
            this.sysInstanceIndexorg = 1
            this.sysInstanceTotalPagesorg = 10

            this.buildings = []
            this.sysInstanceIndexParent = 1
            this.sysInstanceTotalPagesParent = 10

            this.floor = []
            this.sysInstanceIndexfloor = 1
            this.sysInstanceTotalPagesfloor = 10

            this.spaceList = []
            this.sysInstanceIndexspace = 1
            this.sysInstanceTotalPagesspace = 10

            this.deviceList = []
            this.sysInstanceIndexdevice = 1
            this.sysInstanceTotalPagesdevice = 10

            this.enterpriseList = []
            this.sysInstanceIndex = 1
            this.sysInstanceTotalPages = 10

            this.tenantIdflag = true;
            this.deviceIdflag = true;

            this.tenantFlag = false;
            this.orgFlag = false;
            this.parentFlag = false;
            this.floorFlag = false;
            this.spaceFlag = false;
            this.deviceFlag = false;

            await this.getDataList();

        },
    },
};